<template>
  <div class="fillcontain">
    <head-top></head-top>
    <div class="table_container">
      <el-table
        :data="tableData"
        @expand="expand"
        :expand-row-keys="expendRow"
        :row-key="row => row.index"
        style="width: 100%"
      >
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="商品类型">
                <span>{{ props.row.foodLists}}</span>
              </el-form-item>
              <el-form-item label="用户要求">
                <span>{{props.row.specs||"千山万水总是情，送个鸡腿行不性?" }}</span>
              </el-form-item>
              <el-form-item label="包装费">
                <span>{{ props.row.packing_fee}}</span>
              </el-form-item>
              <el-form-item label="配送费">
                <span>{{ props.row.deliver_fee}}</span>
              </el-form-item>
              <el-form-item label="用户名">
                <span>{{ props.row.user_name }}</span>
              </el-form-item>
              <el-form-item label="联系电话">
                <span>{{ props.row.phone+" "+props.row.phone_bk }}</span>
              </el-form-item>
              <!-- <el-form-item label="店铺名称">
                <span>{{ props.row.restaurant_name }}</span>
              </el-form-item>-->
              <el-form-item label="收货地址">
                <span>{{ props.row.address+props.row.address_detail}}</span>
              </el-form-item>
              <el-form-item label="地址类型">
                <span>{{ props.row.tag}}</span>
              </el-form-item>
              <!-- <el-form-item label="店铺 ID">
                <span>{{ props.row.restaurant_id }}</span>
              </el-form-item>
              <el-form-item label="店铺地址">
                <span>{{ props.row.restaurant_address }}</span>
              </el-form-item>-->
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="日期" prop="formatted_created_at"></el-table-column>
        <el-table-column label="总价格" prop="total_amount"></el-table-column>
        <el-table-column label="订单状态" prop="order_status"></el-table-column>
        <el-table-column label="支付方式" prop="payment"></el-table-column>
        <!-- 接单 -->
        <el-table-column fixed="right" label="确认接单" width="100">
          <template slot-scope="scope">
            <el-button
              @click="handleClick(scope.$index)"
              v-if="handleClickList.indexOf(scope.$index) ==-1 && (unConfirmList.indexOf(scope.$index) == -1)"
              type="warning"
              size="small"
            >请接单</el-button>
            <el-button
              type="warning"
              disabled
              v-if="!(handleClickList.indexOf(scope.$index) == -1)"
              size="small"
            >已接单</el-button>
            <el-button
              type="danger"
              disabled
              v-if="!(unConfirmList.indexOf(scope.$index) == -1)"
              size="small"
            >已拒绝</el-button>
          </template>
        </el-table-column>
        <!-- 拒绝接单 -->
        <el-table-column fixed="right" label="拒绝接单" width="100">
          <template slot-scope="scope">
            <el-button
              @click="unConfirmClick(scope.$index)"
              v-if="unConfirmList.indexOf(scope.$index) == -1 && (handleClickList.indexOf(scope.$index) == -1)"
              type="danger"
              size="small"
            >不接单</el-button>
            <el-button
              type="danger"
              disabled
              v-if="!(unConfirmList.indexOf(scope.$index) == -1)"
              size="small"
            >已拒绝</el-button>
            <el-button
              type="warning"
              disabled
              v-if="!(handleClickList.indexOf(scope.$index) == -1)"
              size="small"
            >已接单</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="Pagination" style="text-align: left;margin-top: 10px;">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="20"
          layout="total, prev, pager, next"
          :total="count"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import headTop from "../components/headTop";
import {
  getOrderList,
  getOrderCount,
  getResturantDetail,
  getUserInfo,
  getAddressById,
} from "@/api/getData";
export default {
  data() {
    return {
      tableData: [],
      currentRow: null,
      offset: 0,
      limit: 2000,
      count: 0,
      currentPage: 1,
      restaurant_id: null,
      expendRow: [],
      disabled: true,
      handleClickList: [],
      unConfirmList: [],
      sum_order: 0,
    };
  },
  components: {
    headTop,
  },
  created() {
    this.restaurant_id = this.$route.query.restaurant_id;
    // console.log(this.restaurant_id);
    this.initData();
  },
  mounted() {},
  methods: {
    async initData() {
      try {
        const countData = await getOrderCount({
          restaurant_id: this.restaurant_id,
        });
        if (countData.status == 1) {
          this.count = countData.count;
        } else {
          throw new Error("获取数据失败");
        }
        this.getOrders();
        this.count = this.sum_order;
      } catch (err) {
        console.log("获取数据失败", err);
      }
    },
    // 确认订单
    handleClick(index) {
      this.handleClickList.push(index);
      //   console.log(this.handleClickList);
      //   this.tableData.order_status = "商家已接单";
      //   console.log(this.handleClickList.indexOf(index));
      //   this.disabled = false;
    },
    // 拒绝接单
    unConfirmClick(index) {
      this.unConfirmList.push(index);
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.offset = (val - 1) * this.limit;
      this.getOrders();
      this.handleClickList = [];
      this.unConfirmList = [];
    },
    async getOrders() {
      const Orders = await getOrderList({
        offset: this.offset,
        limit: this.limit,
        restaurant_id: this.restaurant_id,
      });
      this.tableData = [];
      Orders.forEach((item, index) => {
        // console.log(this.$store.state.shop_id);
        if (item.restaurant_id === this.$store.state.shop__id) {
          this.count += 1;
          this.sum_order = this.sum_order + 1;
          const tableData = {};
          tableData.id = item.id;
          tableData.foodList = item.basket.group[0];
          tableData.foodLists = "";
          tableData.specs = "";
          for (let index = 0; index < tableData.foodList.length; index++) {
            tableData.foodLists += tableData.foodList[index].name + "(";
            tableData.foodLists += tableData.foodList[index].price + "元)x";
            tableData.foodLists += tableData.foodList[index].quantity + " ";
            tableData.specs += tableData.foodList[index].specs;
            tableData.specs += tableData.foodList[index].new_specs;
          }
          tableData.packing_fee = item.basket.packing_fee.price;
          tableData.deliver_fee = item.basket.deliver_fee.price;
          tableData.total_quantity = item.total_quantity;
          tableData.formatted_created_at = item.formatted_created_at;
          tableData.total_amount = item.total_amount;
          tableData.status = item.status_bar.title;
          tableData.user_id = item.user_id;
          tableData.restaurant_id = item.restaurant_id;
          tableData.address_id = item.address_id;
          // add
          (tableData.address_detail = item.address_detail),
            (tableData.phone = item.phone),
            (tableData.phone_bk = item.phone_bk),
            (tableData.tag = item.tag),
            (tableData.payment = "在线支付");
          tableData.order_status = "派送中"; //客户已下单；商家制作中；派送中；已完成；未完成
          tableData.index = index;
          this.tableData.push(tableData);
        }
      });
    },
    async expand(row, status) {
      if (status) {
        const restaurant = await getResturantDetail(row.restaurant_id);
        const userInfo = await getUserInfo(row.user_id);
        const addressInfo = await getAddressById(row.address_id);

        this.tableData.splice(row.index, 1, {
          ...row,
          ...{
            restaurant_name: restaurant.name,
            restaurant_address: restaurant.address,
            address: addressInfo.address,
            //
            address_detail: addressInfo.address_detail,
            phone: addressInfo.phone,
            phone_bk: addressInfo.phone_bk,
            tag: addressInfo.tag,
            //
            user_name: userInfo.username,
          },
        });
        this.$nextTick(() => {
          this.expendRow.push(row.index);
        });
      } else {
        const index = this.expendRow.indexOf(row.index);
        this.expendRow.splice(index, 1);
      }
    },
  },
};
</script>

<style lang="less">
@import "../style/mixin";
.table_container {
  padding: 20px;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
